//
//  PhotoListViewController.m
//  Pacific_Computer
//
//  Created by 范琦 on 14-12-17.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "PhotoListViewController.h"
#import "PhotoCell.h"
#import "Photos_List.h"
#import "UIImageView+WebCache.h"
#import "NetHandler.h"
#import "UIScrollView+Extension.h"
#import "UIScrollView+MJRefresh.h"
#import "PhotodetaliViewController.h"


@interface PhotoListViewController ()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic,retain) NSMutableArray *arr;
@property (nonatomic,retain) NSMutableArray *array;
@property (nonatomic,retain) UITableView *tabView;

@property (nonatomic,assign) NSInteger num;

@end

@implementation PhotoListViewController

- (void)dealloc
{
    [_arr release];
    [_array release];
    [_tabView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        
    }
    return self;
}

- (void)handlerData
{
    NSString *path = @"http://mrobot.pconline.com.cn/v2/photo/albums?groupId=41";
    [NetHandler getDataWithUrl:path completion:^(NSData *data) {
       
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSArray *imageArr = [result objectForKey:@"groups"];
        self.array = [NSMutableArray array];
        
        NSMutableArray *numArr = [NSMutableArray array];
        for (NSDictionary *dic in imageArr) {
            Photos_List *photo = [[Photos_List alloc] initWithDictionary:dic];
            [numArr addObject:photo];
            [photo release];
            
            if (numArr.count == 30) {

                [self.array addObject:numArr];
                numArr = [NSMutableArray array];
                
            }
        
            
        }
        self.arr = [NSMutableArray array];
        NSMutableArray *ak = [self.array objectAtIndex:0];
        for (Photos_List *photo in ak) {
            [self.arr addObject:photo];
        }
        
        [self.tabView reloadData];
        

    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    
    //self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];

    
    //self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
    
    
    self.tabView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * 9.59 / 100, self.view.bounds.size.width, self.view.bounds.size.height * 83 / 100) style:UITableViewStylePlain];
    self.tabView.delegate = self;
    self.tabView.rowHeight = self.view.bounds.size.height * 34 / 100;
    self.tabView.dataSource = self;
    [self.tabView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

    //self.tabView.bounces = NO;
    [self.view addSubview:self.tabView];
    [_tabView release];
    [self handlerData];
    [self setupRefresh];
    UIView *view2 = [[UIView alloc] initWithFrame:self.tabView.bounds];
    view2.backgroundColor= [UIColor blackColor];
    
    [self.tabView setBackgroundView:view2];
    [view2 release];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    UIImage *image = [UIImage imageNamed:@"24.jpg"];
    imageView.image = image;
    self.tabView.backgroundView = imageView;
    imageView.userInteractionEnabled = YES;
    self.tabView.backgroundColor = [UIColor clearColor];
    
}





- (void)setupRefresh
{
    
    //    NSLog(@"进入了setupRefresh");
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    
    [self.tabView addHeaderWithTarget:self action:@selector(headerRereshing)];
 
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tabView addFooterWithTarget:self action:@selector(footerRereshing)];
}

- (void)headerRereshing
{
    
    //1. 在这调用请求网络数据方法（刷新数据）
    [self handlerData];
    
    
    
    //2. 2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tabView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tabView headerEndRefreshing];
    });
}


//   进入加载状态
- (void)footerRereshing
{
    self.num = self.arr.count / 30;
    if (self.num < self.array.count) {
        NSArray *arr = [self.array objectAtIndex:self.num];
        for (Photos_List *photo in arr) {
            [self.arr addObject:photo];
        }
    }
    
    
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tabView reloadData];
        [self.tabView footerEndRefreshing];
    });
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"huanhe";
    PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[[PhotoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
    }
    Photos_List *photo = [self.arr objectAtIndex:indexPath.row];
    
    NSString *imageUrl = photo.cover;
    NSURL *url = [NSURL URLWithString:imageUrl];
    
    [cell.myImage setImageWithURL:url];
    cell.titleLabel.text = photo.name;
    
    NSString *countStr = [NSString stringWithFormat:@"%@",photo.photoCount];
    cell.numLabel.text = countStr;
    
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    PhotodetaliViewController *deta = [[PhotodetaliViewController alloc] init];
    deta.hidesBottomBarWhenPushed = YES;
    deta.photo = [self.arr objectAtIndex:indexPath.row];
    //deta.navigationController.navigationBarHidden = YES;
    [self.navigationController pushViewController:deta animated:YES];
    [deta release];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
